// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import Routers from './router/router'
import Vuex from 'vuex'
import '../style.css'
import productData from '../product'

Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(Vuex)

// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: Routers
}
const router = new VueRouter(RouterConfig)

router.beforeEach((to, from, next) => {
  window.document.title = to.meta.title
  next()
})

router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
})

const store = new Vuex.Store({
  state: {
    // 商品列表数据
    productList: [],
    // 购物车数据
    carList: []
  },
  getters: {},
  mutations: {
    // 添加商品列表
    setProductList (state, data) {
      state.productList = data
    }
  },
  actions: {
    // 请求商品列表
    getProductList (context) {
      setTimeout(() => {
        context.commit('setProductList', productData)
      }, 500)
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => {
    return h(App)
  }
})
